import java.util.Random;

public class RandomUtil {
    private static final String[] GENERATE_SOURCE = new String[]{
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private static final int STR_LEN = GENERATE_SOURCE.length;

    public static String generateByRandom(final int length) {
        StringBuilder stringBuilder = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equals(charOrNum)) {
                // 判断字母大小写
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                stringBuilder.append((char) (choice + random.nextInt(26)));
            } else {
                stringBuilder.append(random.nextInt(10));
            }
        }
        return stringBuilder.toString();
    }
}